package com.hx.user.config;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.Data;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 数据源配置类
 *
 * @author lsl
 * 2018-10-04
 */
@Configuration
@Data
@EnableTransactionManagement
@MapperScan(basePackages = {"com.hx.user"})
public class DataBaseConfiguration {

    //mapper 扫描路径
    static final String MapperConfigClassPath = "classpath:mapper/*.xml";
    @Autowired
    Environment environment;
    @Value("${spring.datasource.initialSize}")
    private String initialSize;
    @Value("${spring.datasource.minIdle}")
    private String minIdle;
    @Value("${spring.datasource.maxActive}")
    private String maxActive;
    @Value("${spring.datasource.maxWait}")
    private String maxWait;
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private String timeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private String minEvictableIdleTimeMillis;
    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;
    @Value("${spring.datasource.testOnBorrow}")
    private String testOnBorrow;
    @Value("${spring.datasource.testOnReturn}")
    private String testOnReturn;
    @Value("${spring.datasource.testWhileIdle}")
    private String testWhileIdle;
    @Value("${spring.datasource.poolPreparedStatements}")
    private String poolPreparedStatements;
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private String maxPoolPreparedStatementPerConnectionSize;
    @Value("${spring.datasource.filters}")
    private String filters;

    // 考虑到之后要做多数据源暂时考虑这么写
    @Value("${master.driver-class-name}")
    private String driverClassName;
    @Value("${master.datasource.url}")
    private String url;
    @Value("${master.username}")
    private String username;
    @Value("${master.data-password}")
    private String password;

    @Bean
    public DataSource dataSource() throws Exception {
        Properties properties = new Properties();
        properties.put("driverClassName", driverClassName);
        properties.put("url", url);
        properties.put("username", username);
        properties.put("password", password);
        properties.put("initialSize", initialSize);
        properties.put("minIdle", minIdle);
        properties.put("maxActive", maxActive);
        properties.put("maxWait", maxWait);
        properties.put("timeBetweenEvictionRunsMillis", timeBetweenEvictionRunsMillis);
        properties.put("minEvictableIdleTimeMillis", minEvictableIdleTimeMillis);
        properties.put("validationQuery", validationQuery);
        properties.put("testOnBorrow", testOnBorrow);
        properties.put("testOnReturn", testOnReturn);
        properties.put("testWhileIdle", testWhileIdle);
        properties.put("poolPreparedStatements", poolPreparedStatements);
        properties.put("maxPoolPreparedStatementPerConnectionSize",
                maxPoolPreparedStatementPerConnectionSize);
        properties.put("filters", filters);
        return DruidDataSourceFactory.createDataSource(properties);
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws Exception {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SqlSessionFactory masterSqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MapperConfigClassPath));
        return sqlSessionFactory.getObject();
    }


}
